// pages/login/login.js
import tool from '../../modules/tool'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginType: 0,
    form: {
      phone: '',
      role: '',
      password: '',
      account: '',
      vcode: '',
      username: '',
      departmentId: '',
      departmentName: ''
    },
    error: '',
    vcodeText: '发送验证码',
    handleSendVCodeWithDebounce: () => { }
  },

  handleBackToLogin() {
    tool.postFrontStatus(`点击返回登录界面`)
    wx.redirectTo({
      url: '/pages/login/login',
    })
  },

  async handleClickReg() {
    tool.flow(async () => {
      tool.postFrontStatus(`点击注册按钮`)
      if (!(this.data.form.username && this.data.form.username.length < 16)) throw '用户名不能短于1位长于16位'
      if (!(this.data.form.account && this.data.form.account.length < 32)) throw '账号不能短于1位长于32位'
      if (!tool.check.password(this.data.form.password)) throw '密码不能短于8位和长于16位'
      if (!tool.check.phone(this.data.form.phone)) throw '输入正确的手机号'
      if (this.data.form.vcode.length != 6) throw '输入验证码'
      if (!this.data.form.role) throw '选择角色'
      if (!this.data.form.departmentName) throw '选择部门'
      {
        let re = await tool.requestData('/user/login', {
          code: await tool.getLoginCode(),
          userInfo: (() => {
            let t = this.data.form
            return t
          })()
        })
        if (re.status == 1) {
          if (re.data.disabled) {
            tool.handleShowModel(re.data.content, false, () => { tool.redirectToIndex() })
          } else {
            tool.handleShowToastWithMask(re.data.content, 'success')
            await tool.wait(0.5)
            tool.redirectToIndex()
          }
        } else {
          throw re.content
        }
      }
    }, (e) => tool.handleShowToast(e, 'error'))
  },

  async handleSendVCodeInsideMethod() {
    if (!tool.check.phone(this.data.form.phone)) {
      tool.handleShowToast('输入手机号', 'error')
      return
    }
    if (this.data.vcodeText != '发送验证码') {

      return
    }
    this.setData({
      vcodeText: `正在发送`
    })
    let re = await tool.requestData('/user/getVCode', {
      phone: this.data.form.phone
    }, 'get')
    if (re.status == 1) {
      for (let i = 59; i >= 0; i--) {
        await tool.wait(1)
        this.setData({
          vcodeText: `${i}秒后可重发`
        })
      }
      this.setData({
        vcodeText: `发送验证码`
      })
    } else {
      tool.handleShowToast(re.content, 'error')
    }
  },

  async handleSendVCode() {
    this.data.handleSendVCodeWithDebounce()
  },

  async handleChangeRole() {
    let re = await tool.requestData('/setting/get', {
      key: 'role'
    }, 'get')
    if (re.status == 1) {
      tool.handleSelect('选择角色', re.data, undefined, (item) => {
        wx.navigateBack()
        this.setData({
          form: (() => {
            let t = this.data.form
            t.role = item
            return t
          })()
        })
      })
    } else {
      tool.handleShowToast(re.content, 'error')
    }
  },

  async handleChangeDepartment() {
    let re = await tool.requestData('/user/getDepartmentList', {

    }, 'get')
    if (re.status == 1) {
      tool.handleSelect('选择部门', re.data, 'name', (item) => {
        wx.navigateBack()
        this.setData({
          form: (() => {
            let t = this.data.form
            t.departmentName = item.name
            t.departmentId = item.id
            return t
          })()
        })
      })
    } else {
      tool.handleShowToast(re.content, 'error')
    }
  },

  handleInput(e) {
    let t = this.data.form
    t[e.currentTarget.dataset.field] = e.detail.value
    this.setData({
      form: t
    })
  },

  handleChangeLoginType(e) {
    e = e.currentTarget.dataset.type
    this.setData({
      loginType: e
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    tool.setPageData(this)
    this.setData({
      handleSendVCodeWithDebounce: tool.debounce(this.handleSendVCodeInsideMethod)
    })
    tool.postFrontStatus(`注册界面`)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})